package com.itheima.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.dao.UserDao;
import com.itheima.domain.ResultInfo;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.util.MyBatisUtils;
import com.itheima.util.SmsUtils;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {

    // 注册
    @Override
    public ResultInfo register(User param) {

        // 获取 UserDao代理对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 1.根据username查询dao，校验
        User u1 = userDao.findByUsername(param.getUsername());
        if (u1 != null) {
            // 关闭sqlSession
            MyBatisUtils.close(sqlSession);
            return new ResultInfo(false, "用户名已存在");
        }
        // 2.根据telephone查询dao，校验
        User u2 = userDao.findByTelephone(param.getTelephone());
        if (u2 != null) {
            // 关闭sqlSession
            MyBatisUtils.close(sqlSession);
            return new ResultInfo(false, "手机号已注册");
        }
        // 3.对密码进行加密 （糊涂工具包提供 SecureUtil ）
        String md5Pwd = SecureUtil.md5(param.getPassword());
        param.setPassword(md5Pwd);

        // 4.保存user，完成注册
        userDao.save(param);
        // 关闭sqlSession
        MyBatisUtils.close(sqlSession);
        return new ResultInfo(true);

    }

    // 根据用户名查询
    @Override
    public User findByUsername(String username) {
        // 获取 UserDao代理对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        User user = userDao.findByUsername(username);

        // 关闭sqlSession
        MyBatisUtils.close(sqlSession);
        return user;
    }

    @Override
    public User findByTelephone(String telephone) {
        // 获取 UserDao代理对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = userDao.findByTelephone(telephone);
        // 关闭sqlSession
        MyBatisUtils.close(sqlSession);
        return user;
    }


    // 发送短信
    @Override
    public ResultInfo sendSms(String telephone, String smsCode) {

        // 定义签名
        String signName = "卖鸟平台";
        // 定义模板
        String templateCode = "SMS_190278525";

        // 拼接验证码（json）
        String param = "{\"code\":\"" + smsCode + "\"}";



        // 调用工具类发送短信
//        try {
//            SendSmsResponse sendSmsResponse = SmsUtils.sendSms(telephone, signName, templateCode, param);
//            if (sendSmsResponse.getCode().equals("OK")) {
//                return new ResultInfo(true, "短信发送成功");
//            }
//
//
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//
//        return new ResultInfo(false, "短信发送失败");
        // 测试期间可以写伪代码...
      return new ResultInfo(true, "短信发送成功");
    }


    @Override
    public ResultInfo pwdLogin(User param) {
        // 获取 UserDao代理对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        // 1.根据用户名查询，校验
        User currentUser = userDao.findByUsername(param.getUsername());
        if (currentUser == null) {
            // 关闭sqlSession
            MyBatisUtils.close(sqlSession);
            return new ResultInfo(false, "用户名不存在");
        }
        // 2.校验密码
        // 2.1 将用户输入的进行加密
        String md5 = SecureUtil.md5(param.getPassword());
        // 2.2 对比数据库
        if (!currentUser.getPassword().equals(md5)) {
            // 关闭sqlSession
            MyBatisUtils.close(sqlSession);
            return new ResultInfo(false, "密码不正确");
        }

        // 关闭sqlSession
        MyBatisUtils.close(sqlSession);
        // 3.登录成功
        return new ResultInfo(true, "登录成功", currentUser);
    }

    @Override
    public void updateInfo(User param) {
        //获取UserDao实现对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //调用dao
        userDao.update(param);
        //关闭sqlsession
        MyBatisUtils.close(sqlSession);
    }

    @Override
    public User findByUid(int uid) {
        //调用UserDao代理对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //调用Dao
        User user=userDao.findByUid(uid);
        //关闭sqlSession
        MyBatisUtils.close(sqlSession);
        return user;
    }

}